package ui.panels.primary.adaptiveAgentProperties;

import gameEngine.agents.choosingStrategy.representation.ManuallAdaptiveRepresentation;
import gameEngine.agents.choosingStrategy.representation.StrategyRepresentation;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.table.DefaultTableModel;

import ui.flowManager.expirimentProperties.ExperimentProperties;
import ui.flowManager.expirimentProperties.ExperimentProperties.Agent_Group;
import ui.panels.commands.SavePlayerCommand;
import ui.panels.errors.ErrorFrame;
import ui.panels.errors.ErrorPanel;
import Data.ConfigurationParameters;
import auxiliary.AuxiliaryClass;

public class AdaptivePropertiesListener implements ActionListener{
	
	private AdaptiveAgentProperties myPanel = null;
	private ExperimentProperties 	props = null;
	
	public AdaptivePropertiesListener(AdaptiveAgentProperties panel, ExperimentProperties props)
	{
		this.myPanel 	= panel;
		this.props 		= props;
	}
	
	public void actionPerformed(ActionEvent event) {
		
		//case is the addStrategy button
		if(event.getSource() == myPanel.getAddStrategy())
		{	
			//add new strategy to the table
			myPanel.addRow(ConfigurationParameters.Strategy.EGREEDY);
		}
		
		//case it is save player button, should save
		else if(event.getSource() == myPanel.getSave())
		{
			//call to the function that do the operation
			saveButtonFunc();
		}
		
		else if (event.getSource() == myPanel.getAdditionalSettingsButton())
		{
			myPanel.additionalSettingsButtonFunc(props);
		}
	}
	
	
	private void saveButtonFunc()
	{
	
		// create and execute the save command
		SavePlayerCommand saveCommand = new SavePlayerCommand(); 
		
		//check that the game and the player exist
		if((myPanel.getGameNumber() == 0) || (!props.havePlayers(myPanel.getGameNumber(), Agent_Group.STUDIED_AGENT)))
			return;
		
		if(!AuxiliaryClass.isDouble(myPanel.getEpsilonField().getText()))
			return;
		double eps = Double.parseDouble(myPanel.getEpsilonField().getText());
		
		if(!AuxiliaryClass.isDouble(myPanel.getCompetenceField().getText()))
			return;
		double competence = Double.parseDouble(myPanel.getCompetenceField().getText());
		
		//case invalid values epsilon and competence	
		if((eps > 1)||(eps < 0)||(competence > 1)||(competence < 0))
		{
			ErrorFrame frame = new ErrorFrame();
			frame.add(new ErrorPanel(frame, "Invalid value for Epsilon or Competence"));
			return;
		}
		
		try{
			//--------------	set the data ----------------
			saveCommand.setProps(props);
			
			saveCommand.setGameNumber(myPanel.getGameNumber());
			saveCommand.setPlayerNumber(myPanel.getPlayerNumber());
			// distribution that was selected in the drop
			saveCommand.setDistribution(myPanel.getDistRepresentation());	
			saveCommand.setTeammateModel((String)myPanel.getTeammatedrop().getSelectedItem());
			saveCommand.setAnalyzer((String) myPanel.getAnalyzerDrop().getSelectedItem());	
			
			saveCommand.setEpsilonStr(myPanel.getEpsilonField().getText());
			saveCommand.setCompetenceStr(myPanel.getCompetenceField().getText());
			saveCommand.setAdditionalSettings(myPanel.getAdditionalSettings());
			saveCommand.setGroup(Agent_Group.STUDIED_AGENT);
			
			//take the strategy
			ManuallAdaptiveRepresentation mAdaptiveRepresentation = createStrategy();
			if (mAdaptiveRepresentation == null)
				return;
			saveCommand.setStrategy(mAdaptiveRepresentation);
			//take the next players if the check box is selected
			//--------------	execute ----------------
			saveCommand.execute();
			
			//Mark for the user that the player was saved
			myPanel.labelToBlack();
		
		}
		catch (Exception e){
			
		}
		

	}
	
	
	private ManuallAdaptiveRepresentation createStrategy()
	{
		//save the strategies
		ManuallAdaptiveRepresentation mAdaptiveRepresentation = new 
					ManuallAdaptiveRepresentation(ConfigurationParameters.Strategy.MANUAL_ADAPTIVE);
			
		DefaultTableModel model = myPanel.getModel();
		//loop over the strategies in the table
		for(int i = 0; i < myPanel.getNumRows(); ++i)
		{
			//get the strategy from the drop box

			String type = (String) model.getValueAt(i, 0);
			StrategyRepresentation sRepresentation = myPanel.createStrategyRepresentation(type);
			
			//case the type not exist 
			if(type == null)
				continue;
								
			//number of turns
			String turnsStr = (String) model.getValueAt(i, 1);
			 if(AuxiliaryClass.isNumber(turnsStr))
			 {
				 int turns = Integer.parseInt(turnsStr);
				 mAdaptiveRepresentation.addStrategy(sRepresentation, turns);
			 }
			 else
				 return null;
		}
		
		return mAdaptiveRepresentation;
	}
}
